package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.job_make;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.power_product;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.powerload_hour;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.powerload_hour_fcst;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.DocFile;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.HourPower;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.MsgMedium;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.HourPowerServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HourPowerServiceImpl implements HourPowerServiceI {

	ProcessRequestController pc = new ProcessRequestController();
	
	@Override
	public List<HourPower> hourPowerLoad(String dateTime) {
		List<powerload_hour_fcst> listData=new ArrayList<powerload_hour_fcst>();
		List<HourPower> resultList=new ArrayList<HourPower>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("load");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[]{"datechar asc"});
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime!=null) {
			strWhere.append(String.format("and extract(year from datechar)='%s'",dateTime.substring(0, 4)));
			strWhere.append(String.format("and extract(month from datechar)='%s'",dateTime.substring(5, 7)));
			strWhere.append(String.format("and extract(day from datechar)='%s'",dateTime.substring(8, 10)));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "powerload_hour_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<powerload_hour_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (powerload_hour_fcst fcst : listData) {
				HourPower powerload=new HourPower();
				powerload.setDataChar(fcst.getDatechar());
				powerload.setLoad(fcst.getLoad());
				resultList.add(powerload);
			}
		}
		return resultList;
	}

	@Override
	public List<DocFile> fileHourPowerLoad(String strStartTime, String strEndTime,String hourOrDay) {
		List<power_product> listData=new ArrayList<power_product>();
		List<job_make> listData1=new ArrayList<job_make>();
		List<DocFile> resultList=new ArrayList<DocFile>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("id");
		columns.add("to_char(forecasttime, 'yyyy-MM-dd HH24:MI:SS') as forecasttime");
		columns.add("productname");
		columns.add("producttype");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (strStartTime!=null) {
			strWhere.append(String.format("and forecasttime>='%s'",strStartTime));
		}
		if (strEndTime!=null) {
			strWhere.append(String.format("and forecasttime<='%s'",strEndTime));
		}
		if (hourOrDay!=null && hourOrDay.contains("hour")) {
			strWhere.append(String.format("and producttype='%s'","逐小时电力负荷预报"));
		}
		if (hourOrDay!=null && hourOrDay.contains("day")) {
			strWhere.append(String.format("and producttype='%s'","日电力负荷预报"));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"forecasttime desc"});
		try {
			selectParam.put("method", "power_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<power_product>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (power_product power : listData) {
				DocFile docfile=new DocFile();
				docfile.setId(power.getId());
				docfile.setFileName(power.getProductname());
				Map<String, String> selectParam1 = new HashMap<String, String>();
				RequestDataBySelect selectRequest1 = new RequestDataBySelect();
				List<String> columns1 = new ArrayList<String>();
				columns1.add("productpath");
				selectRequest.setColumns(columns1.toArray(new String[columns1.size()]));
				// 拼接where字段
				StringBuilder strWhere1 = new StringBuilder();
				int whereCount1 = 0;
				if (power.getProducttype()!=null) {
					strWhere1.append(String.format("and jobname='%s'",power.getProducttype()));
				}
				strWhere1.append(String.format("and jobtype='%s'","电力气象"));
				if (whereCount1 == 0 && !StringUtil.IsNullOrEmpty(strWhere1.toString())) {
					strWhere1 = strWhere1.replace(0, 4, "");
				}
				selectRequest1.setParam(strWhere1.toString());
				try {
					selectParam1.put("method", "job_make");
					selectParam1.put("sqlType", "select");
					selectParam1.put("advCode", DbConfig.advCode);
					selectParam1.put("param", JsonUtil.object2Json(selectRequest1));
					String message = pc.processRequest(selectParam1);
					listData1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<job_make>>() {
					});
				} catch (Exception e) {
					// TODO: handle exception
				}
				if (listData1!=null&&listData1.size()>0) {
					for (job_make job : listData1) {
						docfile.setFilePath(job.getProductpath());
					}
				}
				resultList.add(docfile);
			}
		}
		return resultList;
	}

	@Override
	public Date initHourPowerLoadTime() {
		Date date=new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "datechar desc"});
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "powerload_hour_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<powerload_hour_fcst>>() {
			}).get(0).getDatechar();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}
	
	@Override
	public Date initHourPowerLoadTime2() {
		Date date=new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(forecasttime, 'yyyy-MM-dd HH24:MI:SS') as forecasttime"};
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "forecasttime desc"});
		selectRequest.setLimit(1);
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		strWhere.append(String.format("and producttype='%s'","逐小时电力负荷预报"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "power_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<power_product>>() {
			}).get(0).getForecasttime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public Date getHourPowerTime() {
		Date date=new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "datechar desc"});
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "powerload_hour_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<powerload_hour_fcst>>() {
			}).get(0).getDatechar();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public List<HourPower> getHourForecast(String dateTime, String dateTime2) {
		List<powerload_hour_fcst> listData=new ArrayList<powerload_hour_fcst>();
		List<HourPower> resultList=new ArrayList<HourPower>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("load");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[]{"datechar asc"});
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime!=null) {
			strWhere.append(String.format("and datechar>='%s'",dateTime));
		}
		if (dateTime2!=null) {
			strWhere.append(String.format("and datechar<='%s'",dateTime2));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "powerload_hour_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<powerload_hour_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (powerload_hour_fcst fcst : listData) {
				HourPower powerload=new HourPower();
				powerload.setDataChar(fcst.getDatechar());
				powerload.setLoad(fcst.getLoad());
				resultList.add(powerload);
			}
		}
		return resultList;
	}

	@Override
	public List<MsgMedium> getMsgMedium(String dateTime, String dateTime2, boolean b) {
		// TODO Auto-generated method stub
		List<msgmediumsmallscale> listData=new ArrayList<msgmediumsmallscale>();
		List<msgmediumsmallscale> listData1=new ArrayList<msgmediumsmallscale>();
		List<MsgMedium> resultList=new ArrayList<MsgMedium>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(observTime, 'yyyy-MM-dd HH24:MI:SS') as observTime");
		columns.add("replace(cast(dryBulbTemp AS VARCHAR),'-65535','0') as dryBulbTemp");
		columns.add("replace(cast(RelHumidity AS VARCHAR),'-65535','0') as relHumidity");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime!=null) {
			strWhere.append(String.format(" and observTime>='%s'", dateTime));
		}
		if (dateTime2!=null) {
			strWhere.append(String.format(" and observTime<='%s'", dateTime2));
		}
		strWhere.append(String.format(" and stationid='%s'", "57773"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		if(b){
			try {
				selectParam.put("method", "msgmediumsmallscale");
				selectParam.put("sqlType", "select");
				selectParam.put("param", JsonUtil.object2Json(selectRequest));
				String message = pc.processRequest(selectParam);
				listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
				});
				} catch (Exception e) {
				// TODO: handle exception
			}
			if (listData!=null&&listData.size()>0) {
				for (msgmediumsmallscale msg : listData) {
					MsgMedium smallscale=new MsgMedium();
					smallscale.setObservTime(msg.getObservtime());
					smallscale.setDryBulbTemp(msg.getDrybulbtemp().toString());
					smallscale.setRelHumidity(msg.getRelhumidity().toString());
					resultList.add(smallscale);
				}
			}
		}else{
			try {
				selectParam.put("method", "msgmediumsmallscale");
				selectParam.put("sqlType", "select");
				selectParam.put("param", JsonUtil.object2Json(selectRequest));
				String message = pc.processRequest(selectParam);
				listData1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
				});
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (listData1!=null&&listData1.size()>0) {
				for (msgmediumsmallscale msg : listData1) {
					MsgMedium smallscale=new MsgMedium();
					smallscale.setObservTime(msg.getObservtime());
					smallscale.setDryBulbTemp(msg.getDrybulbtemp().toString());
					smallscale.setRelHumidity(msg.getRelhumidity().toString());
					resultList.add(smallscale);
				}
			}
		}
		return resultList;
	}

	@Override
	public List<HourPower> getPower(String dateTime, String dateTime2) {
		List<powerload_hour> listData=new ArrayList<powerload_hour>();
		List<HourPower> resultList=new ArrayList<HourPower>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("load");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[]{"datechar asc"});
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime!=null) {
			strWhere.append(String.format("and datechar>='%s'",dateTime));
		}
		if (dateTime2!=null) {
			strWhere.append(String.format("and datechar<='%s'",dateTime2));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "powerload_hour");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<powerload_hour>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (powerload_hour fcst : listData) {
				HourPower powerload=new HourPower();
				powerload.setDataChar(fcst.getDatechar());
				powerload.setLoad(fcst.getLoad());
				resultList.add(powerload);
			}
		}
		return resultList;
	}
}

